<?php
namespace app\admin\controller;

use app\common\lib\Status as StatusLib;
use think\facade\View;

class Category extends AdminBase {
    public function index(){
        $pid = input('param.pid',0,'intval');
        $data = ['pid'=>$pid];
        try {
            $categorys = (new \app\common\business\Category())->getLists($data, 5);
        } catch (\Exception $e) {
            $categorys = [];
        }
        return View::fetch('', [
            'categorys'=>$categorys,
            'pid'=>$pid
        ]);
    }

    public function add(){
        try {
            $categorys = (new \app\common\business\Category())->getNormalCategorys();
        } catch (\Exception $e) {
            $categorys = [];
        }
        return View::fetch('', [
            'categorys'=>json_encode($categorys)
        ]);
    }

    public function save(){
        $pid = input('param.pid',0,'intval');
        $name = input('param.name',0,'trim');
        $data = [
            'pid'=>$pid,
            'name'=>$name
        ];
        $validate = new \app\admin\validate\Category();
        if ( ! $validate->check($data)){
            return show(config('status.error'), $validate->getError());
        }
        try {
            $result = (new \app\common\business\Category())->add($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if ($result){
            return show(config('status.success'),'OK');
        }
        return show(config('status.error'), '新增分类失败');
    }

    /**
     * 更新排序
     * @return \think\response\Json
     */
    public function listorder(){
        $id = input('param.id',0,'intval');
        $listorder = input('param.listorder',0,'intval');
        if (empty($id)){
            return show(config('status.error'), '参数错误');
        }
        try {
            $res = (new \app\common\business\Category())->listorder($id, $listorder);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if ($res){
            return show(config('status.success'), '排序成功');
        }
        return show(config('status.error'), '排序失败');
    }

    public function status(){
        $id = input('param.id',0,'intval');
        $status = input('param.status',0,'intval');
        if (empty($id) || ! in_array($status, StatusLib::getTableStatus())){
            return show(config('status.error'), '参数错误');
        }
        try {
            $res = (new \app\common\business\Category())->status($id, $status);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if ($res){
            return show(config('status.success'), '状态更新成功');
        }
        return show(config('status.error'), '状态更新失败');
    }

    public function dialog(){
        $categorys = (new \app\common\business\Category())->getNormalByPid();
        return view('', [
            'categorys'=>json_encode($categorys)
        ]);
    }

    public function getByPid(){
        $pid = input('param.pid',0,'intval');
        $categorys = (new \app\common\business\Category())->getNormalByPid($pid);
        return show(config('status.success'), 'OK', $categorys);
    }
}